package com.purdue.edu.psychotron.client.entity;

import java.util.ArrayList;
import java.util.Date;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Entity;
import com.purdue.edu.psychotron.client.entity.ChattingHistory.Builder;

import com.purdue.edu.psychotron.shared.UUID;


@SuppressWarnings("serial")
@Entity
public class ChattingHistory {
	@Id
	String chattingHistoryId;
	String userAId;
	String userAName;
	String userBName;
	String userBId;
	final static String ATOB = "A>B";
	final static String BTOA = "B>A";
	
	
	
	ArrayList<String> historyDirection;
	ArrayList<String> historyContent;
	String historyInStringTEST = "";
	Boolean consecutiveFromAtoB;
	Boolean consecutiveFromBtoA;

	

	public ChattingHistory() {
	}

	public static class Builder {
		private ChattingHistory ChattingHistory = new ChattingHistory();

		public Builder(ChattingHistory chattingHistory) {
			this.ChattingHistory = chattingHistory;
		}

		public Builder(String userAId, String userAName, String userBId, String userBName) {
			this.ChattingHistory = new ChattingHistory();
			this.ChattingHistory.userAId = userAId;
			this.ChattingHistory.userAName = userAName;
			this.ChattingHistory.userBId = userBId;
			this.ChattingHistory.userBName = userBName;
			
			if(userAId.compareTo(userBId) < 0)
				this.ChattingHistory.chattingHistoryId = userAId+userBId;
			else
				this.ChattingHistory.chattingHistoryId = userBId+userAId;
			
			this.ChattingHistory.historyDirection = new ArrayList<String>();
			this.ChattingHistory.historyContent = new ArrayList<String>();
			
			this.ChattingHistory.consecutiveFromAtoB = false;
			this.ChattingHistory.consecutiveFromBtoA = false;
		}


		public Builder addMessage(String senderId, String newMessage) {
			
			if(this.ChattingHistory.historyDirection==null){
				this.ChattingHistory.historyDirection = new ArrayList<String>();
				this.ChattingHistory.historyContent = new ArrayList<String>();
			}
			newMessage = "\r\n"+newMessage;
			
			if(senderId.equals(this.ChattingHistory.userAId)){
				
				this.ChattingHistory.historyDirection.add(ATOB);
			}
			else if (senderId.equals(this.ChattingHistory.userBId)){

				this.ChattingHistory.historyDirection.add(BTOA);
				
			}
			
			this.ChattingHistory.historyContent.add(newMessage);
			
			this.ChattingHistory.historyInStringTEST = this.ChattingHistory.historyContent.toString();
			return this;
		}
		
		public Builder clearHistory(){
			this.ChattingHistory.historyContent.clear();
			this.ChattingHistory.historyDirection.clear();
			this.ChattingHistory.historyDirection = new ArrayList<String>();
			this.ChattingHistory.historyContent = new ArrayList<String>();
			return this;
			
		}
		

		public ChattingHistory build() {
			return this.ChattingHistory;
		}
	}

	public boolean checkIfBothMatch(String user1Id, String user2Id){
		if((user1Id.equals(userAId) || user1Id.equals(userBId)) && (user2Id.equals(userAId) || user2Id.equals(userBId)) ){
			return true;
		}
		return false;
	}

	public String getUserAId() {
		return userAId;
	}

	public String getUserBId() {
		return userBId;
	}
	
	public String getChattingHistoryId() {
		return chattingHistoryId;
	}

	public String getToggledHistoryInString(){
		if(historyDirection==null)
			return "";
		
		String buffer = "";
		for(int i=0; i<historyDirection.size(); i++){
			buffer += formatHistoryUnit(historyDirection.get(i), historyContent.get(i));
		}
		
		return buffer;
	}
	
	public String getHistoryInString(){
		if(historyDirection==null)
			return "";
		String buffer = "";
		for(int i=0; i<historyDirection.size(); i++){
			buffer += historyContent.get(i);
		}
		return buffer;
	}
	
	public String formatHistoryUnit(String dir, String content){
		String buffer = "";
		String direction = "";
		if(dir.equals(ATOB)){
			direction = userAName + " to " + userBName;
		}else{
			direction = userBName + " to " + userAName;
		}
		Date time = new Date();
		buffer = time.toString() + " - " + direction + " \r\n\t" +content;
		return buffer;

	}

	public String getUserAName() {
		return userAName;
	}

	public String getUserBName() {
		return userBName;
	}
	
	public int getHistorySize(){
		return historyDirection.size();
	}
}